package com.whz.generic.swagger;

import com.whz.generic.Constants;
import com.whz.generic.env.EnvProfileEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author whz
 */
@EnableOpenApi
@Configuration
@ConditionalOnProperty(name = Constants.OPTION_SWAGGER3, havingValue = "true", matchIfMissing = true)
public class Swagger3Configuration {

    @Value("${spring.profiles.active:dev}")
    String env;

    @Value("${generic.swagger.scan.package:generic}")
    String basePackage;

    @Bean
    public Docket createSwagger3Api() {
        return new Docket(DocumentationType.OAS_30)
                .enable(!EnvProfileEnum.prod.name().equalsIgnoreCase(env))
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private static ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Open-Api 接口文档")
                .description("swagger3")
                .version("1.0")
                .build();
    }
}